<template>
    <div class="tabs-container">
        <el-tabs v-model="tab" type="card" closable @tab-remove="removeTab" @tab-click="clickTab" class="custom-tabs">
            <el-tab-pane v-for="tab in tabs" :key="tab.name" :label="tab.title" :name="tab.name">

            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script>

export default {
    // eslint-disable-next-line vue/multi-word-component-names
    name: 'Tabs',
    computed: {
        tabs: {
            get() {
                return this.$store.state.tabs
            },
            set(val) {
                this.$store.state.tabs = val
            }

        },
        tab: {
            get() {
                return this.$store.state.tab
            },
            set(val) {
                this.$store.state.tab = val
            }


        },

    }, methods: {
        clickTab(item) {
            if (item.name != this.$route.name) {
                this.tab = item.name
                this.$router.push({
                    path: '/' + item.name
                })
            }

        },
        removeTab(item) {
            let tabList = this.tabs
            let activeTab = this.tab
            if (activeTab == "Home") {
                return;
            }
            if (item == "Home" && this.$route.name != "Home") {

                this.$router.push({
                    path: '/Home'
                })
            }
            if (item == "Home" && this.$route.name == "Home") {
                return;
            }
            if (item == activeTab) {
                tabList.forEach((tab, index) => {
                    if (tab.name == activeTab) {
                        if (index == tabList.length - 1) {
                            activeTab = tabList[index - 1].name
                            tabList.splice(index, 1)
                            this.tab = activeTab
                            this.tabs = tabList
                            this.$router.push({
                                path: '/' + activeTab
                            })
                        } else {
                            activeTab = tabList[index + 1].name
                            tabList.splice(index, 1)
                            this.tab = activeTab
                            this.tabs = tabList
                            this.$router.push({
                                path: '/' + activeTab
                            })

                        }
                    }
                })
            } else {
                tabList.forEach((tab, index) => {
                    if (tab.name == item) {
                        tabList.splice(index, 1)
                    }
                })
            }
        },
        clickfirst() {
            let activeTab = this.tab
            if (activeTab == "Home" && this.$route.name != "Home") {
                this.$router.push({
                    path: '/' + activeTab
                })
            }
        },
    }, mounted() {
        this.clickfirst()
    }
}
</script>

<style>

.tabs-container {
  background-color: #fff;
  padding: 0 10px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
}

.custom-tabs {
  height: 40px;

}

.custom-tabs ::v-deep .el-tabs__header {
  margin: 0;
}

.custom-tabs ::v-deep .el-tabs__nav-wrap::after {
  display: none;
}

.custom-tabs ::v-deep .el-tabs__item {
  height: 40px;
  line-height: 40px;
  padding: 0 20px;
  border: 1px solid #e4e7ed;
  border-bottom: none;
  border-left: none;
  background-color: #f5f7fa;
  color: #606266;
  transition: all 0.3s;
}

.custom-tabs ::v-deep .el-tabs__item:first-child {
  border-left: 1px solid #e4e7ed;
  border-top-left-radius: 4px;
}

.custom-tabs ::v-deep .el-tabs__item:last-child {
  border-top-right-radius: 4px;
}

.custom-tabs ::v-deep .el-tabs__item.is-active {
  background-color: #fff;
  color: #409eff;
  border-bottom: none;
  font-weight: 500;
}

.custom-tabs ::v-deep .el-tabs__item:hover {
  color: #409eff;
  background-color: #ecf5ff;
}

.custom-tabs ::v-deep .el-tabs__item .el-icon-close {
  border-radius: 50%;
  transition: all 0.3s;
}

.custom-tabs ::v-deep .el-tabs__item .el-icon-close:hover {
  background-color: #b4bccc;
  color: #fff;
}
</style>